from flask import Flask, jsonify
from flask_cors import CORS
from lib.db.mgr import DBMgr
from lib.util.log import Log
from lib.web.rs import Resource
from lib.util.tools import MyTools, EncryptionTool
import os

application = app = Flask(__name__)
CORS(app, supports_credentials=True)

current_path = os.getcwd().replace('\\','/')
log_path = '%s/log' % current_path
if not os.path.exists(log_path):
    os.mkdir(log_path)
Log.setup('%s/err.log' % log_path, with_console=True)

source_path = '%s/source/' % current_path
if not os.path.exists(source_path):
    os.mkdir(source_path)

EncryptionTool.initAES(b'\x05\x1e.b\xbf\xfbw/\xa4\x84\xfaT{\\5\xda\xfa/3\x01\xd1\xea\xd8\xca,.Zg\xc6!\x1e\xfa')

app.config.from_pyfile('conf.py')
app.config['SECRET_KEY'] = '0n7UEaAjiG3GD3Rgi4uRpgoTTcusV2fOZf9CTyfWGvP6RibO2kRr6Ba7IVnArpyknJGfjjCiJOTwFSfbSmNEkPwbAjZqwBExOStyLRbqgKCgOScVEqIVYOacteBqbFOi'
app.config['source_path'] = source_path

Resource.setParams(app.config)
try:
    DBMgr.setPool({
        'host': app.config.get('DBHOST', '127.0.0.1'),
        'port': app.config.get('DBPORT', 3306),
        'user': app.config.get('DBUSER', ''),
        'password': EncryptionTool.decryptAES('4mDdHuTRFH2YJKI6F9uK+Q=='),
        'database': app.config.get('DBNAME', '')
    })
except  Exception as e:
    Log.get().error('DB Error: %s' % str(e))
    raise Exception
MyTools.setup()

from gfisp_app.route.main import main
from gfisp_app.route.misc import misc
from gfisp_app.route.user import user

app.register_blueprint(main,url_prefix='/')
app.register_blueprint(misc, url_prefix='/')
app.register_blueprint(user, url_prefix='/')



if __name__ == '__main__':  app.run(port=5051, host='0.0.0.0', debug=True, threaded=True)